<?php
if ( !defined('IN_QUOTE') && !defined('IN_ADMIN') )
{
	die('Hacking attempt');
	exit;
}

if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['Groups']['groups_configuration'] = './admin.php?mode=groups_configuration';
	return;
} else {	

	function edit_group($group_id)
	{
		global $group_info, $lang, $db, $template, $colors, $constants_group_cant_edit;
		if (!$group_id)
		{
			dysplay_groups();
		} else {
			if (!$group_info['can_edit_group'])
			{
				error_message($lang['information'], $lang['cant_edit_group'], ADMIN_URL, '?mode=groups_configuration');		
			} 
			else 
			{	
				$sql = 'SELECT * 
									FROM ' . PERMISSIONS_TABLE . '
									WHERE group_id = ' . $group_id;
				if( !($res = $db->query($sql)) )
				{
					message_die(__LINE__, __FILE__, $sql);
				} 
				else 
				{
					$array = $db->fetcharray($res);
					$submit = request_var('submit', TYPE_NO_HTML);
					
					$group_name = ($submit) ? request_var('group_name') : $array['group_name'];
					$group_color = ($submit) ? request_var('group_color') : $array['group_color'];
					$can_access_quote = ($submit) ? request_var('can_access_quote', TYPE_INT) : $array['can_access_quote'];
					$can_access_memberlist = ($submit) ? request_var('can_access_memberlist', TYPE_INT) : $array['can_access_memberlist'];
					$can_post_quote = ($submit) ? request_var('can_post_quote', TYPE_INT) : $array['can_post_quote'];
					$can_edit_his_quote = ($submit) ? request_var('can_edit_his_quote', TYPE_INT) : $array['can_edit_his_quote'];
					$can_edit_all_quote =  ($submit) ? request_var('can_edit_all_quote', TYPE_INT) : $array['can_edit_all_quote'];
					$can_delete_his_quote = ($submit) ? request_var('can_delete_his_quote', TYPE_INT) : $array['can_delete_his_quote'];
					$can_delete_all_quote = ($submit) ? request_var('can_delete_all_quote', TYPE_INT) : $array['can_delete_all_quote'];
		
					$can_access_admin = ($submit) ? request_var('can_access_admin', TYPE_INT) : $array['can_access_admin'];
					$can_edit_admin_config = ($submit) ? request_var('can_edit_admin_config', TYPE_INT) : $array['can_edit_admin_config'];
					$can_edit_user = ($submit) ? request_var('can_edit_user', TYPE_INT) : $array['can_edit_user'];
					$can_delete_user = ($submit) ? request_var('can_delete_user', TYPE_INT) : $array['can_delete_user'];
					$can_add_user = ($submit) ? request_var('can_add_user', TYPE_INT) : $array['can_add_user'];
					$can_edit_group = ($submit) ? request_var('can_edit_group', TYPE_INT) : $array['can_edit_group'];
					$can_create_group = ($submit) ? request_var('can_create_group', TYPE_INT) : $array['can_create_group'];
					$can_delete_group = ($submit) ? request_var('can_delete_group', TYPE_INT) : $array['can_delete_group'];
					$can_edit_users_permissions = ($submit) ? request_var('can_edit_users_permissions', TYPE_INT) : $array['can_edit_users_permissions'];
					$can_add_news = ($submit) ? request_var('can_add_news', TYPE_INT) : $array['can_add_news'];
					$can_edit_news = ($submit) ? request_var('can_edit_news', TYPE_INT) : $array['can_edit_news'];
					$can_del_news = ($submit) ? request_var('can_del_news', TYPE_INT) : $array['can_del_news'];  
					$can_edit_colors = ($submit) ? request_var('can_edit_colors', TYPE_INT) : $array['can_edit_colors']; 
					$can_delete_colors = ($submit) ? request_var('can_delete_colors', TYPE_INT) : $array['can_delete_colors']; 
					$can_add_colors = ($submit) ? request_var('can_add_colors', TYPE_INT) : $array['can_add_colors']; 
					
					if (!$submit)
					{
						$template->assign('SELECT_BOX', $colors->select_colors($group_color, 'group_color', 0));
						$template->assign('YES_NO', array($lang['Yes'], $lang['No']));
						$template->assign('NUM', array(1,0));	
						$template->assign('CONSTANTS_GROUP_CANT_EDIT', $constants_group_cant_edit);
						$template->assign('GROUP_NAME', $group_name);
						//$template->assign('GROUP_COLOR', $group_color);
						$template->assign('GROUP_ID', $group_id);	
			
						$template->assign('NORMAL_PERMISSIONS', array($can_access_quote, $can_access_memberlist, $can_post_quote, $can_edit_his_quote, $can_edit_all_quote, $can_delete_his_quote, $can_delete_all_quote));
						$template->assign('NORMAL_PERMISSIONS_LANGUAGES', array($lang['Can_access_quote'], $lang['can_access_memberlist'], $lang['can_post_quote'], $lang['can_edit_his_quote'], $lang['can_edit_all_quote'], $lang['can_delete_his_quote'], $lang['can_delete_all_quote']));
						$template->assign('ADMIN_PERMISSIONS', array($can_access_admin, $can_edit_admin_config, $can_edit_user, $can_delete_user, $can_add_user, $can_edit_group, $can_delete_group, $can_create_group, $can_edit_users_permissions, $can_add_news, $can_edit_news, $can_del_news, $can_edit_colors, $can_edit_colors, $can_add_colors));
						$template->assign('ADMIN_PERMISSIONS_LANGUAGES', array($lang['can_access_admin'], $lang['can_edit_admin_config'], $lang['can_edit_user'], $lang['can_delete_user'], $lang['can_add_user'], $lang['can_edit_group'], $lang['can_delete_group'], $lang['can_create_group'], $lang['can_edit_users_permissions'], $lang['can_add_news'], $lang['can_edit_news'], $lang['can_del_news'], $lang['can_add_colors'], $lang['can_edit_colors'], $lang['can_delete_colors']));
		
						$template->assign('NORMAL_PERMISSIONS_KEYS', array('can_access_quote', 'can_access_memberlist', 'can_post_quote', 'can_edit_his_quote', 'can_edit_all_quote', 'can_delete_his_quote', 'can_delete_all_quote'));
						$template->assign('ADMIN_PERMISSIONS_KEYS', array('can_access_admin', 'can_edit_admin_config', 'can_edit_user', 'can_delete_user', 'can_add_user', 'can_edit_group', 'can_delete_group', 'can_create_group', 'can_edit_users_permissions', 'can_add_news', 'can_edit_news', 'can_del_news', 'can_add_colors', 'can_edit_colors', 'can_delete_colors'));
		
						$template->assign('S_GROUP_EDIT_ACTION', './admin.php?mode=groups_configuration&amp;mode_groups=edit&group_id=' . $group_id);
		
			
						$template->display('admin/create_edit_group.tpl');
					}
					else
					{
						if (!$group_name)
						{
							error_message($lang['information'], $lang['No_group_name_entered'], ADMIN_URL, '?mode=groups_configuration');		
						} else {
							if (!in_array($group_id, $constants_group_cant_edit))
							{
								$sql = 'UPDATE ' . PERMISSIONS_TABLE . " SET
													group_name = '" . str_replace("\'", "''", $group_name) . "',
													group_color = '$group_color',  
													can_access_quote = $can_access_quote,
													can_access_memberlist = $can_access_memberlist,
													can_post_quote = $can_post_quote,
													can_edit_his_quote = $can_edit_his_quote,
													can_edit_all_quote = $can_edit_all_quote,
													can_delete_his_quote = $can_delete_his_quote,
													can_delete_all_quote = $can_delete_all_quote,
													can_access_admin = $can_access_admin,
													can_edit_admin_config = $can_edit_admin_config,
													can_edit_user = $can_edit_user,
													can_delete_user = $can_delete_user,
													can_add_user = $can_add_user,
													can_edit_group = $can_edit_group,
													can_delete_group = $can_delete_group,
													can_create_group = $can_create_group,
													can_edit_users_permissions = $can_edit_users_permissions,
													can_add_news = $can_add_news,
													can_edit_news = $can_edit_news,
													can_del_news = $can_del_news,
													can_edit_colors = $can_edit_colors,
													can_delete_colors = $can_delete_colors,
													can_add_colors = $can_add_colors
												WHERE group_id = $group_id";
							} else {
								$sql = 'UPDATE ' . PERMISSIONS_TABLE . " SET
													group_name = '" . str_replace("\'", "''", $group_name) . "',
													group_color = '$group_color'  
												WHERE group_id = $group_id";			
							}
							if( !($res = $db->query($sql)) )
							{
								message_die(__LINE__, __FILE__, $sql);
							} else {
								error_message($lang['information'], $lang['good_edit_groups'], ADMIN_URL, '?mode=groups_configuration');
								$cache->sql_del(PERMISSIONS_TABLE);
								$cache->sql_del('ids_groups');
								$cache->sql_del('legend');
							}	
						}			
					}
				}
			}
		}
	}
	
	function delete_group($group_id)
	{
		global $group_info, $lang, $constants_group, $db, $cache;
	
		if (!$group_id)
		{
			dysplay_groups();
		} else {
			if (!$group_info['can_delete_group'])
			{
						error_message($lang['information'], $lang['cant_delete_group'], ADMIN_URL, '?mode=groups_configuration');		
			} else {
				if (in_array($group_id, $constants_group))
				{
					error_message($lang['information'], $lang['cant_edit_constants_groups'], ADMIN_URL, '?mode=groups_configuration');
				} else {
				
				$sql = 'UPDATE ' . USERS_TABLE . '
					SET user_group = 2
					WHERE user_group IN (' . $group_id . ')';
					if( !($res = $db->query($sql)) )
					{
						message_die(__LINE__, __FILE__, $sql);
					}
					$sql = 'DELETE FROM ' . PERMISSIONS_TABLE . '
										WHERE group_id = ' . $group_id;
					if( !($db->query($sql)) )
					{
						message_die(__LINE__, __FILE__, $sql);
					} else {
						error_message($lang['information'], $lang['good_del_group'], ADMIN_URL, '?mode=groups_configuration');
						$cache->sql_del(PERMISSIONS_TABLE);
						$cache->sql_del('ids_groups');
						$cache->sql_del('legend');
					}	
				}
			}
		}
	}
	
	
	function dysplay_groups()
	{
		global $template, $constants_group, $db, $lang, $group_data, $id_color, $group_info, $board_config, $template_path;
		
		$i=0;
		foreach($constants_group as $group_id)
		{
			$groups = $group_data[$group_id];
			$groups_constants[$i] = $group_data[$i]['group_id'];
			$row_class_constants[$i] = ( !($i % 2) ) ? 'row1' : 'row2';
			$u_edit_constants[$i] = ($group_info['can_edit_group']) ? "<a href=\"./admin.php?mode=groups_configuration&mode_groups=edit&amp;group_id=" . $group_data[$i]['group_id'] . "\"><img src=\"./templates/" . $template_path . "/images/" . $board_config['default_lang'] . "/icon_edit.gif\" alt=\"" . $lang['Edit_groups'] . "\" title=\"" . $lang['Edit_groups'] . "\" border=\"0\"></a>" : '';
			if ($id_color[$group_data[$i]['group_color']])
			{
				$group_name_constants[$i] = (isset($lang[$group_data[$i]['group_name']]) ? '<span style="color:#' . $id_color[$group_data[$i]['group_color']] . '; font-weight: bold;\">' . $lang[$groups['group_name']] . '</span>' : '<span style="color:#' . $array['group_color'] . '; font-weight: bold;\">' . $array['group_name'] . '</span>'); 
			} else {
				$group_name_constants[$i] = (isset($lang[$group_data[$i]['group_name']]) ? $lang[$group_data[$i]['group_name']] : $group_data[$i]['group_name']);
			}
			$i++;
		}
	/* Groupes constants */
		$template->assign('ROW_CLASS_CONSTANT', $row_class_constants);
		$template->assign('ROW_NUMBER_CONSTANT', $groups_constants);
		$template->assign('GROUP_NAME_CONSTANT', $group_name_constants);
		$template->assign('U_EDIT_CONSTANT', $u_edit_constants);
	
		$sql = 'SELECT *
						FROM ' . PERMISSIONS_TABLE . '
						WHERE group_id > 3';		
		if( !($res = $db->query($sql)) )
		{
			message_die(__LINE__, __FILE__, $sql);
		} else {
			$i=0;
			$row_class_variable = array();
			$row_number_variable = array();
			$group_name_variable = array();
			$u_edit_variable = array();
			$u_del_variable = array();
			while ($array = $db->fetcharray($res) )
			{
				$row_class_variable[$i] = ( !($i % 2) ) ? 'row1' : 'row2';
				$row_number_variable[$i] = $array['group_id'];
	
				if (isset($id_color[$array['group_color']]))
				{
					$group_name_variable[$i] = (isset($lang[$array['group_name']]) ? '<span style="color:#' . $id_color[$array['group_color']] . '; font-weight: bold;\">' . $lang[$array['group_name']] . '</span>' : '<span style="color:#' . $array['group_color'] . '; font-weight: bold;\">' . $array['group_name'] . '</span>');
				} else {
					$group_name_variable[$i] = (isset($lang[$array['group_name']]) ? $lang[$array['group_name']] : $array['group_name']);
				}
	
				$u_edit_variable[$i] = ($group_info['can_edit_group']) ? "<a href=\"./admin.php?mode=groups_configuration&mode_groups=edit&amp;group_id=" . $array['group_id'] . "\"><img src=\"./templates/" . $template_path . "/images/" . $board_config['default_lang'] . "/icon_edit.gif\" alt=\"" . $lang['Edit_groups'] . "\" title=\"" . $lang['Edit_groups'] . "\" border=\"0\"></a>" : '';
				$u_del_variable[$i] = ($group_info['can_delete_group']) ? "<a href=\"./admin.php?mode=groups_configuration&mode_groups=delete&amp;group_id=" . $array['group_id'] . "\"><img src=\"./templates/" . $template_path . "/images/" . $board_config['default_lang'] . "/icon_delete.gif\" alt=\"" . $lang['Del_group'] . "\" title=\"" . $lang['Del_group'] . "\" border=\"0\"></a>" : '';
				$i++;
			}
	
	/* Groupes variables */					
			$template->assign('ROW_CLASS_VARIABLE', $row_class_variable);
			$template->assign('ROW_NUMBER_VARIABLE', $row_number_variable);
			$template->assign('GROUP_NAME_VARIABLE', $group_name_variable);	
			$template->assign('U_EDIT_VARIABLE', $u_edit_variable);
			$template->assign('U_DELETE_VARIABLE', $u_del_variable);
			$template->assign('LANG', $board_config['default_lang']);		
			$template->assign('U_CREATE_GROUP', './admin.php?mode=groups_configuration&amp;mode_groups=create');
			$template->assign('CAN_CREATE_GROUP', $group_info['can_create_group']);				
		
			$template->display('admin/groups_configuration.tpl');
		}
	}
	
	function create_group()
	{
		global $group_info, $lang, $db, $template, $constants_group_cant_edit, $colors, $cache;
	
		if (!$group_info['can_create_group'])
		{
					error_message($lang['information'], $lang['cant_create_group'], ADMIN_URL, '?mode=groups_configuration');		
		} else {	
			$submit = request_var('submit', TYPE_NO_HTML);
			if ($submit)
			{
				$group_name = request_var('group_name');
	
				if (!$group_name)
				{
					error_message($lang['information'], $lang['No_group_name_entered'], ADMIN_URL, '?mode=groups_configuration&mode_groups=create');		
				} else {
					$group_color = request_var('group_color');
					$can_access_quote = request_var('can_access_quote', TYPE_INT);
					$can_access_memberlist = request_var('can_access_memberlist', TYPE_INT);
					$can_post_quote = request_var('can_post_quote', TYPE_INT);
					$can_edit_his_quote = request_var('can_edit_his_quote', TYPE_INT);
					$can_edit_all_quote =  request_var('can_edit_all_quote', TYPE_INT);
					$can_delete_his_quote = request_var('can_delete_his_quote', TYPE_INT);
					$can_delete_all_quote = request_var('can_delete_all_quote', TYPE_INT);
		
					$can_access_admin = request_var('can_access_admin', TYPE_INT);
					$can_edit_admin_config = request_var('can_edit_admin_config', TYPE_INT);
					$can_edit_user = request_var('can_edit_user', TYPE_INT);
					$can_delete_user = request_var('can_delete_user', TYPE_INT);
					$can_add_user = request_var('can_add_user', TYPE_INT);
					$can_edit_group = request_var('can_edit_group', TYPE_INT);
					$can_create_group = request_var('can_create_group', TYPE_INT);
					$can_delete_group = request_var('can_delete_group', TYPE_INT);
					$can_edit_users_permissions = request_var('can_edit_users_permissions', TYPE_INT);
					$can_add_news = request_var('can_add_news', TYPE_INT);
					$can_edit_news = request_var('can_edit_news', TYPE_INT);
					$can_del_news = request_var('can_del_news', TYPE_INT);
					$can_edit_colors = request_var('can_edit_colors', TYPE_INT);
					$can_delete_colors = request_var('can_delete_colors', TYPE_INT);
					$can_add_colors = request_var('can_add_colors', TYPE_INT);
	
					$sql = 'INSERT INTO ' . PERMISSIONS_TABLE . " (group_name, group_color, can_access_quote, can_access_memberlist, can_post_quote, can_edit_his_quote, can_edit_all_quote, can_delete_his_quote, can_delete_all_quote, can_access_admin, can_edit_admin_config, can_edit_user, can_delete_user, can_add_user, can_edit_group, can_delete_group, can_create_group, can_edit_users_permissions, can_add_news, can_edit_news, can_del_news, can_edit_colors, can_delete_colors, can_add_colors)
										VALUE ('" . str_replace("\'", "''", $group_name) . "', '$group_color', $can_access_quote, $can_access_memberlist, $can_post_quote, $can_edit_his_quote, $can_edit_all_quote, $can_delete_his_quote, $can_delete_all_quote, $can_access_admin, $can_edit_admin_config, $can_edit_user, $can_delete_user, $can_add_user, $can_edit_group, $can_delete_group, $can_create_group, $can_edit_users_permissions, $can_add_news, $can_edit_news, $can_del_news, $can_add_colors, $can_edit_colors, $can_delete_colors)";
					if( !($res = $db->query($sql)) )
					{
						message_die(__LINE__, __FILE__, $sql);
					} else {
						error_message($lang['information'], $lang['good_add_groups'], ADMIN_URL, '?mode=groups_configuration');
						$cache->sql_del(PERMISSIONS_TABLE);
						$cache->sql_del('ids_groups');
						$cache->sql_del('legend');
					}	
				}
			} else {
				$template->assign('SELECT_BOX', $colors->select_colors(0, 'group_color', 0));
				$template->assign('YES_NO', array($lang['Yes'], $lang['No']));
				$template->assign('NUM', array(1,0));	
				$template->assign('CONSTANTS_GROUP_CANT_EDIT', $constants_group_cant_edit);
	
				$template->assign('NORMAL_PERMISSIONS', array(0, 0, 0, 0, 0, 0, 0));
				$template->assign('NORMAL_PERMISSIONS_LANGUAGES', array($lang['Can_access_quote'], $lang['can_access_memberlist'], $lang['can_post_quote'], $lang['can_edit_his_quote'], $lang['can_edit_all_quote'], $lang['can_delete_his_quote'], $lang['can_delete_all_quote']));
				$template->assign('ADMIN_PERMISSIONS', array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
				$template->assign('ADMIN_PERMISSIONS_LANGUAGES', array($lang['can_access_admin'], $lang['can_edit_admin_config'], $lang['can_edit_user'], $lang['can_delete_user'], $lang['can_add_user'], $lang['can_edit_group'], $lang['can_delete_group'], $lang['can_create_group'], $lang['can_edit_users_permissions'], $lang['can_add_news'], $lang['can_edit_news'], $lang['can_del_news'], $lang['can_add_colors'], $lang['can_edit_colors'], $lang['can_delete_colors']));
	
				$template->assign('NORMAL_PERMISSIONS_KEYS', array('can_access_quote', 'can_access_memberlist', 'can_post_quote', 'can_edit_his_quote', 'can_edit_all_quote', 'can_delete_his_quote', 'can_delete_all_quote'));
				$template->assign('ADMIN_PERMISSIONS_KEYS', array('can_access_admin', 'can_edit_admin_config', 'can_edit_user', 'can_delete_user', 'can_add_user', 'can_edit_group', 'can_delete_group', 'can_create_group', 'can_edit_users_permissions', 'can_add_news', 'can_edit_news', 'can_del_news', 'can_add_colors', 'can_edit_colors', 'can_delete_colors'));
	
				$template->assign('S_GROUP_ADD_ACTION', './admin.php?mode=groups_configuration&amp;mode_groups=create');
		
				$template->display('admin/create_edit_group.tpl');
			}
		}
	}
	
	$group_id = request_var('group_id', TYPE_INT);
	$mode_groups = request_var('mode_groups', TYPE_NO_HTML);
	$constants_group = array('0', '1', '2', '3');
	$constants_group_cant_edit = array('1', '3');
	
	switch($mode_groups)
	{
		case 'delete':
			delete_group($group_id);
			break;
		case 'create':
			create_group();
			break;
		case 'edit':
			edit_group($group_id);
			break;
		default:
			dysplay_groups();
			break;
	}
}

?>
